<?php

class MaskController extends Controller
{
		
    private $client; 
			
	function init()
	{
	/*
		ini_set('soap.wsdl_cache_enabled', '0'); 
		ini_set('soap.wsdl_cache_ttl', '0'); 
		parent::init();
	 	$this->client =  new SoapClient('http://services.pec-sg.ch/index.php/webservice/mask?wsdl',
			array('trace' => 1)
			);
		$auth = array(
        	'UserName'=>'Paul',
        	'Password'=> 'qaws'
        	);
  		$header = new SoapHeader('http://services.pec-sg.ch','Auth', $auth ,false);

  		$this->client->__setSoapHeaders($header);
*/
	}

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
		 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	public function actionGenerate($id)
	{
	/*
			$model = new MaskForm;
			if (1==1){ //Yii::app()->request->isAjaxRequest) {
			$model->attributes=$_POST['MaskForm'];
			if ($_POST['mydropdownlist'] == "1") {$list = $model->visaPrefixList();$len = 16;}
			if ($_POST['mydropdownlist'] == "2") {$list = $model->amexPrefixList();$len = 15;}
			if ($_POST['mydropdownlist'] == "3") {$list = $model->mastercardPrefixList();$len = 16;}	
			if ($_POST['mydropdownlist'] == "4") {$list = $model->lasercardPrefixList();$len = 16;}
			//$list = $model->visaPrefixList();$len = 16;
			$CreditCardNumbers = $model->CreditCardNumber($list,$len,1);
			foreach($CreditCardNumbers as $cc){
			    if ($len == 15)
			    	$genCreditCardNumber = substr($cc,0,4).' '.substr($cc,4,6).' '.substr($cc,10,5).'<br />';
			    else
					$genCreditCardNumber = substr($cc,0,4).' '.substr($cc,4,4).' '.substr($cc,8,4).' '.substr($cc,12,4).'<br />';
			}
	        	$maskType = 'scrambleXxn';
	        	if($id == 1) $maskType = 'scrambleXxn';
	        	if($id == 2) $maskType = 'maskCC';
	        
	        	$txt = $this->maskedTexte($model->unmask, $maskType, $_POST['mydropdownlist']);

	        	echo '<table style="width: 90%;><thead class="odd"><th style="width: 30%; text-align:right;">masking pattern</thead><th>masking result</th><tr>';
				echo '<tr><td style="width: 30%; text-align:right;">your input </td><td>'.$txt[0]['unmasked'].'</td></tr>';
			
				if($id == 1) {

					echo '<tr><td style="width: 30%; text-align:right;">masked: A->X a->x 1->n </td><td>'.$txt[1]['masked'].'</td></tr>';
					echo '<tr><td style="width: 30%; text-align:right;">char rotation</td><td>'.$model->rotate().'</td></tr>';

					echo '<tr><td style="width: 30%; text-align:right;">reverse rotation</td><td>'.str_rot13($model->rotate()).
						'</td></tr></table>';
				}
				elseif($id == 2){
					echo '<tr><td style="width: 30%; text-align:right;">masked Credit Card #</td><td>'.$txt[1]['masked'].'</td></tr>';
					echo '<tr><td style="width: 30%; text-align:right;">generated Credit Card #</td><td>'.$genCreditCardNumber.'</td></tr></table>';
				}
        	} 
        	Yii::app()->end();
        	*/
    }
    
public function actionCardNumbers()
{
			$model=new MaskForm;
			if (Yii::app()->request->isAjaxRequest) {
			$model->attributes=$_POST['MaskForm'];
			if ($_POST['mydropdownlist'] == "1") {$list = $model->visaPrefixList();$len = 16;}
			if ($_POST['mydropdownlist'] == "2") {$list = $model->amexPrefixList();$len = 15;}
			if ($_POST['mydropdownlist'] == "3") {$list = $model->mastercardPrefixList();$len = 16;}	
			if ($_POST['mydropdownlist'] == "4") {$list = $model->lasercardPrefixList();$len = 16;}
			$CreditCardNumbers = $model->CreditCardNumber($list,$len,3);
			foreach($CreditCardNumbers as $cc){
			    if ($len == 15)
			    	echo substr($cc,0,4).' '.substr($cc,4,6).' '.substr($cc,10,5).'<br />';
			    else
					echo substr($cc,0,4).' '.substr($cc,4,4).' '.substr($cc,8,4).' '.substr($cc,12,4).'<br />';
			}
			$this->renderPartial('_view', array(
			'dataProvider'=> 'b'), true, true);
			Yii::app()->end();	
			}
}
	public function actionIndex()
	{

		//$model=new MaskForm;;
		//Yii::log('Index', 'info', 'appl');
		$this->render('index'); //,array('model'=>$model, 'page' => 1));

	}
	/**
	 * Displays a particular model.
	 */
	public function actionView($id)
	{
	/*
		$cc = array();
		$model=new MaskForm;
		if($id == 2) {
			$cc = $model->CreditCardNumber($model->visaPrefixList(),16,3);
		}
		$this->render('view', array(
			'page' => $id,
			'model'=> $model,
			'CreditCardNumbers' => $cc,
		));
		*/
	}
	public function maskedTexte($txt,$maskType, $cardType=0){
			$this->client = new SoapClient('http://services.pec-sg.ch/index.php/webservice/mask?wsdl',
			array('trace' => 0)
			);
		$result='';
		$auth = array(
        	'UserName'=>'Paul',
        	'Password'=> 'qaws'
        	);
  		$header = new SoapHeader('http://services.pec-sg.ch','Auth', $auth ,false);

  		$this->client->__setSoapHeaders($header);
		try{
			$result = $this->client->getMaskedText(CJSON::encode(array('maskType'=> $maskType, 'Txt'=>$txt, 'cardType'=> $cardType)));
		}
	 	catch (Exception $e) {  
    		return $e->getMessage(); 
		} 
		return  CJSON::decode($result, true);
	}
	
	public function anonymTexte($txt){
		$result='';
		try{
			$result = $this->client->getScrambled(CJSON::encode(array('Txt'=>$txt)));
		}
	 	catch (Exception $e) {  
    		return $e->getMessage(); 
		} 
		return  CJSON::decode($result, true);
	}


}
